//
//  WFStatusRetweetView.swift
//  WangFangWeiBo
//
//  Created by 王方 on 16/7/14.
//  Copyright © 2016年 wf. All rights reserved.
//

import UIKit
import SnapKit
class WFStatusRetweetView: UIView {
    
    //记录转发微博底部的约束
    var retweetViewBottom:Constraint?
    
    var statusViewModel:WFStatusViewModel?{
        didSet {
//            retweetContentLabel.text = statusViewModel?.retweetContent
            retweetContentLabel.attributedText = statusViewModel?.retweetAttributedStr
            
            //卸载上一次的约束
            retweetViewBottom?.uninstall()
            
            //判断是否有转发微博配图
            if let count = statusViewModel?.status?.retweeted_status?.pic_urls?.count where count > 0 {
                //说明是有配图的
                //那就设置模型
                
                pictureView.picUrls = statusViewModel?.status?.retweeted_status?.pic_urls
//                print(statusViewModel?.status?.retweeted_status?.pic_urls.self)
                self.snp_updateConstraints(closure: { (make) -> Void in
                    self.retweetViewBottom = make.bottom.equalTo(pictureView).offset(HomeCellMargin).constraint
                    
                    pictureView.hidden = false
                })
                
            }else{
                //说明没有配图
                 self.snp_updateConstraints(closure: { (make) -> Void in
                    self.retweetViewBottom = make.bottom.equalTo(retweetContentLabel).offset(HomeCellMargin).constraint
                 })
                pictureView.hidden = true
            }
        
        }
    }
    
    // MARK:  - 懒加载
    private lazy var retweetContentLabel:UILabel = {
        let label = UILabel(textColor: UIColor.grayColor(), fontSize: HomeFontSize)
        label.numberOfLines = 0
        return label
        }()
    
    //转发微博配图
    private lazy var pictureView:WFStatusPictureView = {
        let collectionView = WFStatusPictureView()
        
        collectionView.backgroundColor = self.backgroundColor
        return collectionView
    }()
    
    // MARK:  - 构造
    override init(frame: CGRect) {
        super.init(frame: frame)
        setUpUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // MARK:  - 初始化控件
    private func setUpUI(){
        backgroundColor = UIColor.whiteColor()
        retweetContentLabel.backgroundColor = UIColor(white: 0.95, alpha: 1)
        
        addSubview(retweetContentLabel)
        addSubview(pictureView)
        
        retweetContentLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(self).offset(HomeCellMargin)
            make.leading.equalTo(self).offset(HomeCellMargin)
            make.trailing.equalTo(self).offset(-HomeCellMargin)
        }
        //转发配图的约束
        pictureView.snp_makeConstraints { (make) -> Void in
        make.leading.equalTo(retweetContentLabel)
            make.top.equalTo(retweetContentLabel.snp_bottom).offset(HomeCellMargin)
        }
        
        //关键约束,决定转发视图的高度
        self.snp_makeConstraints { (make) -> Void in
           self.retweetViewBottom = make.bottom.equalTo(pictureView.snp_bottom).offset(HomeCellMargin).constraint
        }
    }

}
